package com.qgs.waf.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.jedis.JedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
import org.springframework.stereotype.Service;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.qgs.waf.service.RedisService;
import com.qgs.waf.util.CollectionUtil;

import redis.clients.jedis.Jedis;

/**
 * @show 综合情况,数据从redis拉取
 * @author Administrator
 *
 */
@Service("redisServiceImpl")
@SuppressWarnings({ "rawtypes", "unchecked" })
public class RedisServiceImpl implements RedisService {

	@Autowired
	private JedisConnectionFactory jedisConnectionFactory;
	@Autowired
	private RedisTemplate jedisTemplate;
	@Autowired
	private Jedis jedis;

	@Override
	public Map<String, String> getHashSet(String hash_key) {
		JedisConnection jc = jedisConnectionFactory.getConnection();
		Map<byte[], byte[]> map = jc.hGetAll(hash_key.getBytes());
		jc.close();
		return CollectionUtil.mapInByte2MapInObject(map);
	}

	@Override
	public Set<TypedTuple> rangeWithScores(Object key, long min, long max) {
		return jedisTemplate.opsForZSet().rangeWithScores(key, min, max);
	}

	@Override
	public Set rangeByScore(Object key, long min, long max) {
		return jedisTemplate.opsForZSet().rangeByScore(key, min, max);
	}

	@Override
	public JedisConnection getJedisClient() {
		return jedisConnectionFactory.getConnection();
	}

	@Override
	public Set<String> hKeys(String key) {
		Set<String> re_keys = new HashSet<String>();
		JedisConnection jc = jedisConnectionFactory.getConnection();
		Set<byte[]> keys = jc.hKeys(key.getBytes());
		for (byte[] bs : keys) {
			re_keys.add(new String(bs));
		}
		jc.close();
		return re_keys;
	}

	@Override
	public Map<String, String> hGetAll(String key) {
		JedisConnection jc = jedisConnectionFactory.getConnection();
		Map<byte[], byte[]> values = jc.hGetAll(key.getBytes());
		jc.close();
		return CollectionUtil.mapInByte2MapInObject(values);
	}

	@Override
	public byte[] hGet(byte[] key, byte[] field) {
		JedisConnection jc = jedisConnectionFactory.getConnection();
		byte[] hGet = jc.hGet(key, field);
		jc.close();
		return hGet;
	}

	@Override
	public List<byte[]> hMGet(byte[] key, byte[]... fields) {
		JedisConnection jc = jedisConnectionFactory.getConnection();
		List<byte[]> hMGet = jc.hMGet(key, fields);
		jc.close();
		return hMGet;
	}

	@Override
	public Set<byte[]> hKeys(byte[] key) {
		JedisConnection jc = jedisConnectionFactory.getConnection();
		Set<byte[]> hKeys = jc.hKeys(key);
		jc.close();
		return hKeys;
	}

	@Override
	public List<byte[]> hVals(byte[] key) {
		JedisConnection jc = jedisConnectionFactory.getConnection();
		List<byte[]> hVals = jc.hVals(key);
		jc.close();
		return hVals;
	}

	@Override
	public List<String> hMGet(String key, String... fields) {
		return null;
	}

	@Override
	public Jedis getJedis() {
		return jedis;
	}

	@Override
	public List<Map<String, String>> keysAndValues(String key, String pro_key, String pro_value, String... fileds) {
		List<Map<String, String>> vals = new ArrayList<Map<String, String>>();
		Map<String, String> all = hGetAll(key);
		Map<String, String> obj;
		for (String filed : fileds) {
			obj = new TreeMap<String, String>();
			obj.put(pro_key, filed);
			obj.put(pro_value, all.get(filed));
			vals.add(obj);
		}
		return vals;
	}

	@Override
	public String keyAndValue(String key,String pro_key,String pro_value, String... fileds) {
		return keysAndValues4Redis(key, pro_key, pro_value, null, null, fileds);
	}

	@Override
	public String keysAndValues4Redis(String key,String pro_key,String pro_value,String pro_score, Number[] scores, String... fileds) {
		JsonArray ja = new JsonArray();
		Map<String, String> all = hGetAll(key);
		JsonObject jo;
		for (int i = 0; i < fileds.length; i++) {
			jo = new JsonObject();
			jo.addProperty(pro_key, fileds[i]);
			jo.addProperty(pro_value, all.get(fileds[i]));
			if (null != scores && scores[i] != null) {
				jo.addProperty(pro_score, scores[i]);
			} else {
				jo.addProperty(pro_score, new Random().nextInt(2000));
			}
			ja.add(jo);
		}
		return ja.toString();
	}

}
